
from pycuda.compiler import SourceModule

match_module = SourceModule("""

// prototype to match many images to many projections
__global__ void MatchManyMany(float * img, float * ref, int N,
		int * best_idx, float * resid)
	{
	float the_sum = 0.0f;
	float * refptr = &ref[blockIdx.x*N*N];
	float * imgptr = &img[threadIdx.x*N*N];

	for (int i=0; i<N*N; i++) {
		float diff = imgptr[i] - refptr[i];
		the_sum += diff*diff;
		}

	float choice =  resid[threadIdx.x] - the_sum;
	choice = fminf(1.0f,choice);
	choice = fmaxf(0.0f,choice);
	choice = ceilf(choice);

	resid[threadIdx.x] = (1.0f - choice)*resid[threadIdx.x] + choice*the_sum;
	best_idx[threadIdx.x] = (1 - int(choice))*best_idx[threadIdx.x];
	best_idx[threadIdx.x] += int(choice)*threadIdx.x;
	}
	""")


MatchManyMany = match_module.get_function("MatchManyMany")
